<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="100px" size="small">
      <el-form-item label="承运商名称" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          placeholder="请输入承运商名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="请选择状态">
          <el-option
            v-for="dict in dict.type.CommDataStatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="承运商类型" prop="carrierType">
        <el-select v-model="queryParams.carrierType" clearable placeholder="请选择承运商类型">
          <el-option
            v-for="dict in dict.type.CarrierType"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同开始日期">
        <el-date-picker
          v-model="daterangeStartTime"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="合同结束日期">
        <el-date-picker
          v-model="daterangeEndTime"
          end-placeholder="结束日期"
          range-separator="-"
          start-placeholder="开始日期"
          style="width: 240px"
          type="daterange"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['vehicle:carrier:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['vehicle:carrier:edit']"
          :disabled="single"
          icon="el-icon-edit"
          plain
          size="mini"
          type="success"
          @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['vehicle:carrier:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="carrierList" @selection-change="handleSelectionChange">
      <el-table-column align="center" fixed type="selection" width="55"/>
      <el-table-column align="center" label="承运商名称" prop="name"/>
      <el-table-column align="center" label="承运商代码" prop="code"/>
      <el-table-column align="center" label="状态" prop="status">
        <template #default="scope">
          <dict-tag :options="dict.type.CommDataStatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系地址" prop="address"/>
      <el-table-column align="center" label="承运商类型" prop="carrierType">
        <template #default="scope">
          <dict-tag :options="dict.type.CarrierType" :value="scope.row.carrierType"/>
        </template>
      </el-table-column>
      <el-table-column align="center" label="合同开始日期" prop="startTime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="合同结束日期" prop="endTime" width="140">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="承运商电话" prop="iphone"/>
      <el-table-column align="center" label="账期" prop="paymentDays"/>
      <el-table-column align="center" label="负责人" prop="principal"/>
      <el-table-column align="center" label="负责人电话" prop="principalIphone"/>
      <el-table-column align="center" label="组织机构" prop="jcOrganizationIdDtLabel"/>
      <el-table-column align="center" label="指定组织机构" prop="zdOrganizationIdDtLabel"/>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template #default="scope">
          <el-button
            v-hasPermi="['vehicle:carrier:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
            v-hasPermi="['vehicle:carrier:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改承运商对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="承运商名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入承运商名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承运商代码" prop="code">
              <el-input v-model="form.code" placeholder="请输入承运商代码"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入联系地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承运商类型" prop="carrierType">
              <el-select v-model="form.carrierType" placeholder="请选择承运商类型">
                <el-option
                  v-for="dict in dict.type.CarrierType"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item v-if="form.carrierType === 0" label="指定组织机构" prop="zdOrganizationId">
              <el-select v-model="form.zdOrganizationId" clearable filterable placeholder="请选择出发网点">
                <el-option v-for="dict in dict.type.DB_JC_ORG" :key="dict.value" :label="dict.label"
                           :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开始日期" prop="startTime">
              <el-date-picker v-model="form.startTime"
                              clearable
                              placeholder="请选择合同开始日期"
                              type="date"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束日期" prop="endTime">
              <el-date-picker v-model="form.endTime"
                              clearable
                              placeholder="请选择合同结束日期"
                              type="date"
                              value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承运商电话" prop="iphone">
              <el-input v-model="form.iphone" placeholder="请输入承运商电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="账期" prop="paymentDays">
              <el-input-number v-model="form.paymentDays" placeholder="请输入账期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="principal">
              <el-input v-model="form.principal" placeholder="请输入负责人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人电话" prop="principalIphone">
              <el-input v-model="form.principalIphone" placeholder="请输入负责人电话"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio
                  v-for="dict in dict.type.CommDataStatus"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listCarrier, getCarrier, delCarrier, addCarrier, updateCarrier} from "@/api/vehicle/carrier";

export default {
  name: "Carrier",
  dicts: ['CommDataStatus', 'CarrierType', 'DB_JC_ORG'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承运商表格数据
      carrierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 指定组织机构时间范围
      daterangeCreateTime: [],
      // 指定组织机构时间范围
      daterangeModifyTime: [],
      // 指定组织机构时间范围
      daterangeStartTime: [],
      // 指定组织机构时间范围
      daterangeEndTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        status: null,
        address: null,
        carrierType: null,
        startTime: null,
        endTime: null,
        iphone: null,
        paymentDays: null,
        principal: null,
        principalIphone: null,
        jcOrganizationId: null,
        zdOrganizationId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          {required: true, message: "承运商代码不能为空", trigger: "blur"}
        ],
        name: [
          {required: true, message: "承运商名称不能为空", trigger: "blur"}
        ],
        status: [
          {required: true, message: "状态不能为空", trigger: "change"}
        ],
        address: [
          {required: true, message: "联系地址不能为空", trigger: "blur"}
        ],
        carrierType: [
          {required: true, message: "承运商类型不能为空", trigger: "change"}
        ],
        startTime: [
          {required: true, message: "合同开始日期不能为空", trigger: "blur"}
        ],
        endTime: [
          {required: true, message: "合同结束日期不能为空", trigger: "blur"}
        ],
        paymentDays: [
          {required: true, message: "账期不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询承运商列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' !== this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      if (null != this.daterangeModifyTime && '' !== this.daterangeModifyTime) {
        this.queryParams.params["beginModifyTime"] = this.daterangeModifyTime[0];
        this.queryParams.params["endModifyTime"] = this.daterangeModifyTime[1];
      }
      if (null != this.daterangeStartTime && '' !== this.daterangeStartTime) {
        this.queryParams.params["beginStartTime"] = this.daterangeStartTime[0];
        this.queryParams.params["endStartTime"] = this.daterangeStartTime[1];
      }
      if (null != this.daterangeEndTime && '' !== this.daterangeEndTime) {
        this.queryParams.params["beginEndTime"] = this.daterangeEndTime[0];
        this.queryParams.params["endEndTime"] = this.daterangeEndTime[1];
      }
      listCarrier(this.queryParams).then(response => {
        this.carrierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        address: null,
        carrierType: null,
        startTime: null,
        endTime: null,
        iphone: null,
        paymentDays: null,
        principal: null,
        principalIphone: null,
        jcOrganizationId: null,
        zdOrganizationId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeModifyTime = [];
      this.daterangeStartTime = [];
      this.daterangeEndTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加承运商";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCarrier(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改承运商";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCarrier(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCarrier(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除承运商编号为"' + ids + '"的数据项？').then(function () {
        return delCarrier(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
  }
};
</script>
